package com.yuexun.dhdoctor.module.login.presenter.impl;

import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.login.bean.LoginBean;
import com.yuexun.dhdoctor.module.login.bean.UserBean;
import com.yuexun.dhdoctor.module.login.model.ILoginModel;
import com.yuexun.dhdoctor.module.login.presenter.ILoginPresenter;
import com.yuexun.dhdoctor.module.login.ui.view.ILoginView;

import org.kymjs.kjframe.ui.ViewInject;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by work-jx on 2016/7/11.
 */
public class LoginPresneter implements ILoginPresenter {
    private ILoginView view;
    private ILoginModel model;

    //记录Log
//    private final Logger log = ProUtil.getLogger(LoginPresneter.class);

    public LoginPresneter(ILoginModel model, ILoginView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void dologin(Map<String, String> praise) {
        model.doInLogin(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
//                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {
//                        log.info("UserPresenter_doInLogin_onCompleted");
                        view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                        ViewInject.toast("登录失败");
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        view.doInLogin(loginBean);
                    }
                });
    }

    @Override
    public void getUseInfo(Map<String, String> praise) {
        model.getUserInfo(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
//                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UserBean>() {
                    @Override
                    public void onCompleted() {
//                        log.info("UserPresenter_doInLogin_onCompleted");
                        view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        log.info("UserPresenter_doInLogin_onCompleted");
                        view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                        ViewInject.toast("获取个人信息失败");
                    }

                    @Override
                    public void onNext(UserBean userBean) {
                        view.getUserInfo(userBean);
                    }
                });
    }
}
